package com.example.xianzhi.controller;

import cn.hutool.json.JSONUtil;
import com.example.xianzhi.entity.TbGoods;
import com.example.xianzhi.service.GoodsService;
import com.example.xianzhi.service.impl.GoodsServiceImpl;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/goods")
public class GoodsController {

    //查询商品的业务层
    private GoodsService goods = new GoodsServiceImpl();

    @YockMvcAnnotation.RequestMapping("/toGoods.do")
    @YockMvcAnnotation.ResponseDispatch("/WEB-INF/view/bookmanager.jsp")
    public ModelAndView toGoods(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{
        ModelAndView model = new ModelAndView();

        return model;
    }



    /**
     * 查询所有商品信息
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectgoods.do")
    @YockMvcAnnotation.ResponseBody
    public String selectgoods(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品信息的集合
        Map<String, Object> map = new HashMap<>();

        //查询所有商品
        List<TbGoods> tbGoods = goods.selectGoods();
        System.out.println(tbGoods);

        map.put("code",200);
        map.put("data",tbGoods);

        String jsonStr = JSONUtil.toJsonStr(map);

        return jsonStr;
    }


    /**
     * 模糊查询商品
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectgoodsinfo.do")
    @YockMvcAnnotation.ResponseBody
    public String selectgoodsinfo(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "goods_name") String goods_name,
            //这里要把type_id转成String,不然前端传空值过来时，它会报空值异常
            @YockMvcAnnotation.RequestParam(name = "type_id") String type_id2
    ) {

        System.out.println("type_id2 s"+type_id2);
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品信息的集合
        Map<String, Object> map = new HashMap<>();

        int type_id=0;
        if (type_id2!=null && !type_id2.equals("")){
            type_id = Integer.parseInt(type_id2);
        }
        System.out.println(type_id);

        List<TbGoods> tbGoods = goods.selectGoodsinfo(goods_name, type_id);

        map.put("code",200);
        map.put("data",tbGoods);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }


    /**
     * 删除商品
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/deletegoods.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void addtype(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "goods_id") int goods_id
    ) throws Exception{

        goods.deleteGoods(goods_id);

    }


    /**
     * 查询所有订单信息
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/indent1.do")
    @YockMvcAnnotation.ResponseBody
    public String indent1(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品信息的集合
        Map<String, Object> map = new HashMap<>();

        //查询所有订单
        List<TbGoods> indent = goods.selectIndent();


        map.put("code",200);
        map.put("data",indent);

        String jsonStr = JSONUtil.toJsonStr(map);

        return jsonStr;
    }

    @YockMvcAnnotation.RequestMapping("/indent.do")
    @YockMvcAnnotation.ResponseDispatch("/WEB-INF/view/ordermanager.jsp")
    public ModelAndView indent(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        ModelAndView model = new ModelAndView();
        //查询所有订单
        List<TbGoods> indent = goods.selectIndent();
        model.addObject("indent",indent);
        System.out.println(model);
        return model;
    }

    @YockMvcAnnotation.RequestMapping("/delectGoods.do")	//配置二级路径
    @YockMvcAnnotation.ResponseBody
    public String delectGoods(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "goods_id") String goods_id        //商品id
    ) throws Exception{
        // 保存商品信息的集合
        Map<String, Object> map = new HashMap<>();
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        int goods_id2 =0;
        if(goods_id!=null && !goods_id.equals("")){
            goods_id2= Integer.parseInt(goods_id);
            goods.delectGoods(goods_id2);
            map.put("code","200");
            map.put("data","删除成功");
        }else {
            map.put("code","500");
            map.put("data","删除失败");
        }

        String jsonStr = JSONUtil.toJsonStr(map);
        System.out.println(jsonStr);
        return jsonStr;
    }

    /**
     * 查询所有订单信息
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/classification.do")
    @YockMvcAnnotation.ResponseBody
    public String classification(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "publisher") String publisher,       //收件人
            @YockMvcAnnotation.RequestParam(name = "buy_time") String buy_time,       //订单时间
            @YockMvcAnnotation.RequestParam(name = "addressee") String addressee,       //发布人
            @YockMvcAnnotation.RequestParam(name = "goods_id") String goods_id       //订单ID
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品信息的集合
        Map<String, Object> map = new HashMap<>();
        int goods_ids=0;
        if(goods_id!=null && !goods_id.equals("")){
            goods_ids=Integer.parseInt(goods_id);
        }
        //查询所有订单
        List<TbGoods> indent = goods.selectIndentClassification(publisher,buy_time,addressee,goods_ids);


        map.put("code",200);
        map.put("data",indent);

        String jsonStr = JSONUtil.toJsonStr(map);

        return jsonStr;
    }

}
